package exception;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class ThrowDemo01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一个大于0的整数:");
        int i = sc.nextInt();
        if(i<0)
        throw new IllegalArgumentException("您输入的数据不正确");
        System.out.println("您输入的数据为:"+i);
        //将读入的整数写入到文件
        try {
            doWrite(i);
        }catch (IOException e){
            System.out.println("记录写文件的错误日志");
            //抛出非检查异常(编译阶段服务检测到的异常)
            throw new RuntimeException("系统暂时错误，请稍后尝试");
        }
    }
    static void doWrite(int i) throws IOException{
        try(FileOutputStream fos=new FileOutputStream("./fos.txt")){
            fos.write(i);
        }catch (IOException e){
            System.out.println("读写文件失败了");//记录在后台了
            throw e;//直接抛出异常对象
        }
    }
}
